package com.example.boot1.service.impl;

import com.example.boot1.bean.VuePeiCuisineBean;
import com.example.boot1.util.DateUtil;
import com.example.boot1.util.ImageStringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import com.example.boot1.mapper.VueCuisineMapper;
import com.example.boot1.bean.VueCuisine;
import com.example.boot1.service.VueCuisineService;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import java.text.DecimalFormat;
@Service
public class VueCuisineServiceImpl implements VueCuisineService{

    @Value("${server.port}")
    private int port;

    @Autowired
    private VueCuisineMapper vueCuisineMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return vueCuisineMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(VueCuisine record) {
        return vueCuisineMapper.insert(record);
    }

    @Override
    public int insertSelective(VueCuisine record) {
        String tmpImgStr ="";
        List<Map> imglist =  record.getGoodsimage();
        int listSize = imglist.size();
        for (int i = 0; i < listSize; i++) {
            if (i==listSize-1){
                tmpImgStr += imglist.get(i).get("url");
            }else{
                tmpImgStr += imglist.get(i).get("url")+",";
            }
        }
        record.setImageNames(tmpImgStr);
        //首次添加默认赋值
        record.setTime(DateUtil.getStringDate());
        BigDecimal numMs = new BigDecimal("0.0");
        record.setQuantity(0);
        record.setMonthlySale(numMs);
        record.setOnsale(true);
        //将String 转化成 BigDecimal 类型
        String st = record.getUnitprice().toString();
        System.out.println("得到的单价是："+st);
        BigDecimal price = new BigDecimal(st);
        record.setUnitprice(price);

        return vueCuisineMapper.insertSelective(record);
    }

    @Override
    public VueCuisine selectByPrimaryKey(Integer id) {
        return vueCuisineMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(VueCuisine record) {

        String tmpImgStr ="";
        List<Map> imglist =  record.getGoodsimage();
        int listSize = imglist.size();
        for (int i = 0; i < listSize; i++) {
            if (i==listSize-1){
                tmpImgStr += ImageStringUtil.dealHttpStr((String) imglist.get(i).get("url"));
            }else{
                tmpImgStr += ImageStringUtil.dealHttpStr((String) imglist.get(i).get("url"))+",";
            }
        }
        record.setImageNames(tmpImgStr);
        BigDecimal bd = new BigDecimal(record.getUnitprice().toString());
        record.setUnitprice(bd);
        return vueCuisineMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(VueCuisine record) {
        return vueCuisineMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<VueCuisine> findByName(String name) {

        return vueCuisineMapper.findByName(name);
    }

    @Override
    public List<VueCuisine> getAllCuisine(HttpServletRequest request, Integer page) {
        List<VueCuisine> vcl = vueCuisineMapper.getAllCuisine(page);
        //处理
        System.out.println("当前服务端口号："+port);
        System.out.println("当前服务端ip："+request.getRemoteHost());
        String imgUrlBase = "http://"+request.getRemoteHost() + ":"+port+"/image/";
        if (null!=vcl && vcl.size()>0){
            for (int i = 0; i < vcl.size(); i++) {
                List<Map> mlist= new ArrayList<>();

                VueCuisine vc = vcl.get(i);
                String[] imgArr = null;
                if ( vc.getImageNames().contains(",")){
                    imgArr = vc.getImageNames().split(",");
                }
                if (imgArr == null){
                    List<Map> mpl= new ArrayList();
                    Map mp = new LinkedHashMap();
                    mp.put("url",imgUrlBase+vc.getImageNames());
                    mpl.add(mp);
                    vc.setGoodsimage(mpl);
                }else{
                    for (int j = 0; j < imgArr.length; j++) {
                        Map mp= new LinkedHashMap();
                        mp.put("url",imgUrlBase+imgArr[j]);
                        mlist.add(mp);
                    }
                    vc.setGoodsimage(mlist);
                }
            }
        }

        return vcl;
    }

    @Override
    public int findAllTotal() {
        return vueCuisineMapper.findAllTotal();
    }

    @Override
    public List<VueCuisine> findByIdAndName(Integer id, String name) {
        return vueCuisineMapper.findByIdAndName(id,name);
    }

    /**
     * 查询饭店的销量最好的前10菜品
     * @return
     */
    @Override
    public List<VuePeiCuisineBean> findBySaleTop10() {

        //遍历list 给前端造百分比
        List<VuePeiCuisineBean> vpc= vueCuisineMapper.findBySaleTop10();
        DecimalFormat decimalFormat = new DecimalFormat("#.##");


        if (null!=vpc &&  vpc.size()>0){
            int saleNumTotal =0 ;
            int arrSize = vpc.size();
            for (int i = 0; i < arrSize; i++) {
                saleNumTotal += vpc.get(i).getQuantity();
            }
            System.out.println(saleNumTotal);
            for (int i = 0; i < arrSize; i++) {
                VuePeiCuisineBean vb = vpc.get(i);
                int tmpCnum = vb.getQuantity();
                //格式化
                double res = (double)tmpCnum / saleNumTotal;

                String result = decimalFormat.format(res);

                float ptemp = Float.parseFloat(result);

                vb.setPercent(ptemp);

            }
        }
        return vpc;
    }

}
